/**
 * FileResourceHover.java
 * created by wesley at 2008-6-7
 */
package net.sf.filelink.hover;

import java.util.List;

import net.sf.filelink.utl.QueryUtils;
import net.sf.filelink.utl.XmlEditorUtils;

import org.eclipse.core.resources.IFile;
import org.eclipse.jface.internal.text.html.HTMLPrinter;
import org.eclipse.jface.internal.text.html.HTMLTextPresenter;
import org.eclipse.jface.text.DefaultInformationControl;
import org.eclipse.jface.text.DefaultTextHover;
import org.eclipse.jface.text.IInformationControl;
import org.eclipse.jface.text.IInformationControlCreator;
import org.eclipse.jface.text.IRegion;
import org.eclipse.jface.text.ITextHoverExtension;
import org.eclipse.jface.text.ITextViewer;
import org.eclipse.jface.text.source.ISourceViewer;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Shell;
import org.w3c.dom.Node;

/**
 * FileResourceHover
 * 
 * @author wesley
 */
public class FileResourceHover extends DefaultTextHover implements ITextHoverExtension {

    /**
     * constructor for FileReso urceHover
     * 
     * @param sourceViewer
     */
    public FileResourceHover(ISourceViewer sourceViewer) {
        super(sourceViewer);
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.eclipse.jface.text.DefaultTextHover#getHoverInfo(org.eclipse.jface.text.ITextViewer,
     *      org.eclipse.jface.text.IRegion)
     */
    @Override
    public String getHoverInfo(ITextViewer textViewer, IRegion hoverRegion) {

        int offset = hoverRegion.getOffset();
        Node node = XmlEditorUtils.getNodeByOffset(textViewer.getDocument(), offset);

        if (node != null && (node.getNodeType() == Node.TEXT_NODE || node.getNodeType() == Node.ATTRIBUTE_NODE)) {

            String nodeInfo = node.getNodeValue();
            if (nodeInfo != null) {
                nodeInfo = nodeInfo.trim();
            }

            StringBuffer infoBuff = new StringBuffer();
            HTMLPrinter.addPageProlog(infoBuff);
            HTMLPrinter.addParagraph(infoBuff, "Files:");
            infoBuff.append("</p>");

            HTMLPrinter.startBulletList(infoBuff);

            // search file by path
            String filePath = QueryUtils.toFilePath(nodeInfo);
            List<IFile> fileList = QueryUtils.findFileResources(filePath, QueryUtils.getSearchFolders());
            for (IFile file : fileList) {
                String fileInfo = file.getProjectRelativePath().toString();
                HTMLPrinter.addBullet(infoBuff, HTMLPrinter.convertToHTMLContent(fileInfo));
            }

            // search file by name
            String fileName = QueryUtils.toFileName(nodeInfo);
            if (!fileName.equals(filePath)) {
                fileList = QueryUtils.findFileResources(fileName, QueryUtils.getSearchFolders());
                for (IFile file : fileList) {
                    String fileInfo = file.getProjectRelativePath().toString();
                    HTMLPrinter.addBullet(infoBuff, HTMLPrinter.convertToHTMLContent(fileInfo));
                }
            }

            HTMLPrinter.endBulletList(infoBuff);

            HTMLPrinter.addPageEpilog(infoBuff);

            return infoBuff.toString();
        } else {
            return null;
        }
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.eclipse.jface.text.ITextHoverExtension#getHoverControlCreator()
     */
    public IInformationControlCreator getHoverControlCreator() {
        return new IInformationControlCreator() {
            public IInformationControl createInformationControl(Shell parent) {
                int shellStyle = SWT.RESIZE | SWT.TOOL;
                int style = SWT.WRAP;
                IInformationControl controll = new DefaultInformationControl(parent, shellStyle, style,
                        new HTMLTextPresenter());
                return controll;
            }
        };
    }
}
